package com.example.demo.leetcode.top100;

import java.util.ArrayList;
import java.util.List;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _19螺旋矩阵
 * @description
 * @date 2023/06/08 21:55
 * <p>
 * ******************************************************
 */
public class _19螺旋矩阵 {

    public List<Integer> spiralOrder(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        List<Integer> res = new ArrayList<>();

        int upper_bound = 0;
        int lower_bound = m - 1;
        int left_bound = 0;
        int right_bound = n - 1;

        while (res.size() < m * n) {

            if (upper_bound <= lower_bound) {
                for (int i = left_bound; i <= right_bound; i++) {
                    res.add(matrix[upper_bound][i]);
                }
                upper_bound++;
            }

            if (left_bound <= right_bound) {
                for (int i = upper_bound; i <= lower_bound; i++) {
                    res.add(matrix[i][right_bound]);
                }
                right_bound--;
            }

            if (upper_bound <= lower_bound) {
                for (int i = right_bound ; i >= left_bound; i--) {
                    res.add(matrix[lower_bound][i]);
                }
                lower_bound--;
            }

            if (left_bound <= right_bound) {
                for (int i = lower_bound ; i >= upper_bound; i--) {
                    res.add(matrix[i][left_bound]);
                }
                left_bound++;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        _19螺旋矩阵 s = new _19螺旋矩阵();
        s.spiralOrder(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
    }
}
